package c4j.cvs.patch;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.haaproject.beyondcompare.tool.DiffXmlReader;

import c4j.file.FileUtils;

public class BeyondCompareSourcePatcher extends Patcher {

	public static void patchByBeyondCompareXml(String xmlFile, String patchDir, Map<String, String> parameters, HashSet<String> filterFileSet) throws Exception {
		List<CVSRecord> records = listDiffFiles(xmlFile, filterFileSet);
		patch(records, patchDir, parameters);
	}

	public static List<CVSRecord> listDiffFiles(String xmlFile, HashSet<String> filterFileSet) throws Exception {

		final List<CVSRecord> records = new ArrayList<CVSRecord>();
		String date = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss).format(new Date());
		List<String> diffList = DiffXmlReader.readDiffList(xmlFile);

		for (String filePath : diffList) {

			if (filePath.indexOf(File.separator) == -1 || filePath.toLowerCase().matches(".*\\\\test\\\\.*")) {
				continue;
			}

			String fileName = FileUtils.getFileName(filePath);
			if (fileName.toLowerCase().startsWith("test") || fileName.toLowerCase().endsWith(".sql") || filterFileSet.contains(fileName)) {
				continue;
			}

			CVSRecord record = new CVSRecord();

			record.setDate(date);
			record.setAuthor("geln_yang");
			record.setMessage("change");
			record.setFilePath(filePath);
			record.setRevision("");

			records.add(record);

		}

		return records;
	}
}
